package com.gztms.usermgnt.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gztms.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Zoey
 * @date 2023/4/21 22:02
 * @Description TODO
 **/
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入时的填充策略
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开启插入时填充策略");
        String uid = null;
        try {
            uid = SecurityUtils.getUid();
            if (uid != null) {
                this.setFieldValByName("createdBy",uid,metaObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 设置默认状态、性别、头像、删除标志
        this.setFieldValByName("createTime",new Date(), metaObject);
        this.setFieldValByName("status", 0, metaObject);
        this.setFieldValByName("gender",2, metaObject);
        this.setFieldValByName("isDelete",0,metaObject);

        this.setFieldValByName("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif", metaObject);
        this.setFieldValByName("version",1,metaObject);
    }

    /**
     * 更新时的填充策略
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开启更新时填充策略");
        this.setFieldValByName("updateTime",new Date(), metaObject);
//        this.setFieldValByName("",SecurityUtils.getUid(), metaObject);
//        this.setFieldValByName("checkedBy",SecurityUtils.getUid(),metaObject);
    }
}
